package com.zyc.web;


import com.zyc.service.RequestService;
import com.zyc.vo.RequestInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/web/")
@Slf4j
public class WebController {

    // 注解请求信息Service
    @Autowired
    private RequestService requestService;

    @Value("${local.address}")
    private String ipAddress;


    @PostMapping("request")
    public Map<String, Object> saveRequest(HttpServletRequest request, RequestInfo param) {

        param.setIpAddress(ipAddress);
        param.setTotalCount(1);
//        log.info("添加请求信息: " + param);

        // 保存请求信息到数据库
        RequestInfo result = requestService.save(param);
        // 返回保存结果给客户端
        Map<String, Object> map = new HashMap<>();
        if(null != result){
            map.put("result", true);
            map.put("message", "添加请求信息成功!");
        }else{
            map.put("result", false);
            map.put("message", "添加请求信息失败!");
        }

        return map;
    }
}
